<?php
class article extends table
{
	protected $_name	= 'my_article';
	protected $_name_1	= 'my_article_data_1';
	protected $fields	= array('id', 'title', 'ftitle', 	'flag', 'tag', 'tag',	'keyword', 'digest', 'litpic', 'writer', 'classid', 'content', 'sort', 'posttime', 'edittime', 'audit', );
	protected $fields_1	= array('id',	'classid',	'content');
	protected $_primary = 'id';

    public function fetchAll($where=null,$order=null,$count=null,$offset=null)
    {
        $data = parent::fetchAll($where,$order,$count,$offset);
        foreach ($data as $key => $val)
        {
            $data[$key]['flag'] = explode(',',$val['flag']);
        }
        return $data;
        
    }
    
    public function getTempList($list, $columns, $attr=null)
    {
    	/*foreach($list as $key=>$val)
		{
			$column		= $columns[$val['classid']];
			$aid		= $val['id'];
			$newsurl	= !empty($column['domain']) ? $column['domain'].'/' : $column['classpath'];
			$link		= $column['contentextension'] ;
			$url 		= $column['classpath'];

			eval("\$link = \"$link\";");
			eval("\$url = \"$url\";");
			
			$list[$key]['link']			= $link;
			$list[$key]['url']			= $url;
			$list[$key]['classname']	= $column['classname'];
		}
		return $list;
		*/
		if(!is_array($list)) $list	= $list->toArray();
		foreach($list as $key=>$val)
		{
			$column		= $columns[$val['classid']];
			$aid		= $val['id'];
			$newsurl	= !empty($column['domain']) ? $column['domain'] : $attr['url'].$column['classpath'];
			$link		= $column['contentextension'] ;
			$url 		= !empty($column['domain']) ? $column['domain'] : $attr['url'].$column['classpath'];
			
			eval("\$link = \"$link\";");
			eval("\$url = \"$url\";");
			
			$list[$key]['link']			= $link;
			$list[$key]['url']			= $url;
			$list[$key]['classname']	= $column['classname'];
		}
		return $list;
    }
    
    public function getAllByClassid($classid)
    {
    	$db	= self::db();
    	$classid = is_array($classid) ? join(',',$classid) : $classid;
    	$query = "SELECT * from ".$this->_name." as a ,".$this->_name_1." as b WHERE a.id=b.id and a.classid in ($classid)";
    	return $db->fetchAll($query);
    }
    
    public function getAllByid($id)
    {
    	$db	= self::db();
    	$id = is_array($id) ? join(',',$id) : $id;
    	$query = "SELECT * from ".$this->_name." as a ,".$this->_name_1." as b WHERE a.id=b.id and a.id in ($id)";
    	return $db->fetchAll($query);
    }
    
	public function getRow($id)
    {
    	$db	= self::db();
    	$id = is_array($id) ? join(',',$id) : $id;
    	$query = "SELECT * from ".$this->_name." as a ,".$this->_name_1." as b WHERE a.id=b.id and a.id in ($id)";
    	return $db->fetchAll($query);
    }

    public function searchQuery($where)
    {
        $arg = array();
        $arg[] = $this->_name . ".classid={$where['classid']}";
        if ('id'       == $where['field'])
        {
            $arg[] = "id={$where['value']}";
        }
        if ('title'     == $where['field'])
        {
            $arg[] = "title like '%{$where['value']}%'";
        }
        if ('writer'    == $where['field'])
        {
            $arg[] = "writer like '%{$where['value']}%'";
        }
        if ('content'   == $where['field'])
        {
            $arg['from'] = $this->_name . " ," . $this->_name_1;
            $arg[] = $this->_name.".id=".$this->_name_1.".id";
            $arg[] = $this->_name_1.".content like '%{$where['value']}%'";
        }
        isset($where['limit']) && $arg['limit'] = $where['limit'];
        isset($where['order']) && $arg['order'] = $where['order'];
        isset($where['group']) && $arg['group'] = $where['group'];
        #isset($where['limit']) && $arg['limit'] = $where['limit'];
        return $arg;
    }

    public function fetchRowById($id)
    {
        $db = self::db();
        $article    = $db->fetchRow($db->select()->from($this->_name)->from($this->_name_1)->where($this->_name.".id=".$this->_name_1.".id and ".$this->_name.".id=$id"));
        $article['flag'] = explode(',',$article['flag']);
        return $article;
    }

    public function editByid($id, $edit)
    {
        $db                 = self::db();
        $edit['edittime']   = time();
        $fields             = array_get($edit, $this->fields);
        $fields_1           = array_get($edit, $this->fields_1);
        $fields['flag']     = isset($fields['flag']) ? join(',',$fields['flag']) : '';
        $where              = "id = $id ";

        unset($fields['id'], $fields_1['id']);

        $db->update($this->_name, $fields, $where);
        $db->update($this->_name_1, $fields_1, $where);
        return true;
    }

    public function insert($arg)
    {
        $db                 = self::db();
        $fields             = array_get($arg, $this->fields);
        $fields['flag']     = isset($fields['flag']) ? join(',',$fields['flag']) : '';
        $fields['posttime'] = time();
        $fields['edittime'] = time();

        $db->insert($this->_name, $fields);
        $id            = $db->lastInsertId();

        $db->insert($this->_name_1, array('id'=>$id,'classid'=>$arg['classid'],'content'=>$arg['content']));
        return $id;
    }
    
    public function columnList($arg)
    {
    	$show       = $arg['show']	= isset($arg['show']) ? abs($arg['show']) : 20;
        $page       = isset($arg['page']) ? abs($arg['page']) : 1;
        $order      = isset($arg['order']) ? $this->_name.'.'.$arg['order'] : $this->_name.'.id desc';
        
    	if ( isset($arg['field']) )
        {
            $where      = array('classid'=>$arg['classid'],'field'=>$arg['field'],'value'=>$arg['value'],'order'=>$order,'limit'=>"$page,$show");
            $query      = base::init('article')->searchQuery($where);
            $num        = base::init('article')->getNum($query);
            $articles   = base::init('article')->getAll($query);
        }
        else 
        {
            $articles   = base::init('article')->getAll(array('classid'=>$arg['classid'],'limit'=>"$page,$show",'order'=>$order));
            $num        = base::init('article')->getNum(array('classid'=>$arg['classid']));
        }
        return array($articles,$num);
    }
}
